﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Entities.Users;
using DotNetNuke.Entities.Portals;

using ISGPayment;

namespace CharityGamesOnline.Modules.Account
{
    public partial class Pay : System.Web.UI.Page
    {

        /*
         <component type="Config">
          <config>
            <configFile>web.config</configFile>
            <install>
              <configuration>
                <nodes>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="PaymentUrl" value="http://test.tobenefitcharities.org/GoToPay.aspx"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="PaymentEncryptKey" value="ISGPayment"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="PaymentFlag" value="Test"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="ISGAppID" value="CharityGamesOnline"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="ISGGatewayID" value="52501"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="ISGProductKey" value="t001"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="ISGProductUnit" value="12.5"/>
                  </node>         
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="ISGReturnURL" value="http://test.charitygamesonline.org/ISGReturn.aspx"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="ISGCancelURL" value="http://test.charitygamesonline.org/ISGCancel.aspx"/>
                  </node>
                </nodes>
              </configuration>
            </install>
            <uninstall>
              <configuration>
                <nodes />
              </configuration>
            </uninstall>
          </config>
        </component>
         */
        /*
         <component type="Config">
          <config>
            <configFile>web.config</configFile>
            <install>
              <configuration>
                <nodes>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="PaymentUrl" value="http://www.tobenefitcharities.org/GoToPay.aspx"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="PaymentEncryptKey" value="ISGPayment"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="PaymentFlag" value="NotTest"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="ISGAppID" value="CharityGamesOnline"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="ISGGatewayID" value="52501"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="ISGProductKey" value="001"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="ISGProductUnit" value="12.5"/>
                  </node>         
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="ISGReturnURL" value="http://test.charitygamesonline.org/ISGReturn.aspx"/>
                  </node>
                  <node path="/configuration/appSettings" action="update" key="key" collision="overwrite">
                    <add key="ISGCancelURL" value="http://test.charitygamesonline.org/ISGCancel.aspx"/>
                  </node>
                </nodes>
              </configuration>
            </install>
            <uninstall>
              <configuration>
                <nodes />
              </configuration>
            </uninstall>
          </config>
        </component>
         */
        private int paymentID;
        public int PaymentID
        {
            get
            {
                if (paymentID == 0)
                {
                    if ((Request.QueryString["PaymentID"] != null))
                    {
                        paymentID = Int32.Parse(Request.QueryString["PaymentID"]);
                    }
                }
                return paymentID;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            UserInfo objUser = UserController.GetCurrentUserInfo();
            PaymentController ctrlPayment = new PaymentController();
            PaymentInfo objPayment = ctrlPayment.GetPayment(PortalSettings.Current.PortalId, PaymentID);
            decimal productUnit = Convert.ToDecimal(ConfigurationManager.AppSettings["ISGProductUnit"]);
            if (objUser != null && objPayment!=null && objPayment.UserID == objUser.UserID)
            {
                try
                {
                    objPayment.StatusID = (int)PaymentInfo.STATUS.Processing;
                    ctrlPayment.SavePayment(objPayment);

                    ClientPaymentRequest tran = new ClientPaymentRequest(ConfigurationManager.AppSettings["ISGAppID"]);
                    tran.IsTest = ConfigurationManager.AppSettings["PaymentFlag"].ToUpper() == "TEST";
                    tran.GatewayID = ConfigurationManager.AppSettings["ISGGatewayID"];
                    tran.Language = "English";
                    tran.ReturnURL = ConfigurationManager.AppSettings["ISGReturnURL"];
                    tran.CancelURL = ConfigurationManager.AppSettings["ISGCancelURL"];
                    tran.TransactionID = objPayment.PaymentID.ToString();
                    tran.Products.Add(new Product(ConfigurationManager.AppSettings["ISGProductKey"], Convert.ToInt32(objPayment.Amount / productUnit)));

                    tran.Name = objUser.DisplayName;
                    tran.Address = objUser.Profile.Street;
                    tran.City = objUser.Profile.City;
                    tran.State = objUser.Profile.Region;
                    tran.ZipCode = objUser.Profile.PostalCode;
                    tran.Country = objUser.Profile.Country;
                    tran.Phone = objUser.Profile.Telephone;
                    tran.Email = objUser.Email;
                    tran.GoToPay();
                }
                catch (Exception ex)
                {
                    StreamWriter errorLog = new StreamWriter(HttpContext.Current.Server.MapPath(".") + "\\PAY_SEND_ERR.log", true);
                    errorLog.WriteLine(DateTime.Now.ToString());
                    if (objPayment != null)
                    {
                        errorLog.WriteLine(string.Format("PaymentID:{0}", objPayment.PaymentID));
                    }
                    errorLog.WriteLine("Exception Message:");
                    errorLog.WriteLine(ex.Message);
                    errorLog.WriteLine("Exception Trace:");
                    errorLog.WriteLine(ex.StackTrace);
                    errorLog.Close();
                }
                finally
                {
                    PaymentLogInfo objLog = new PaymentLogInfo();
                    if (objPayment != null)
                    {
                        objLog.PaymentID = objPayment.PaymentID;
                        objLog.AppID = ConfigurationManager.AppSettings["ISGAppID"];
                        objLog.IsTest = ConfigurationManager.AppSettings["PaymentFlag"].ToUpper() == "TEST";
                        objLog.ProductKey = ConfigurationManager.AppSettings["ISGProductKey"];
                        objLog.ProductQuantity = Convert.ToInt32(objPayment.Amount / productUnit);
                        objLog.TransactionID = objPayment.PaymentID.ToString();
                        objLog.TransType = "SEND";
                        ctrlPayment.SavePaymentLog(objLog);
                    }
                }
            }
            else
            {
                Response.Write("Invalid User or User is not matched with the current user!");
            }
        }
    }
}
